package zozopo.manual;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;


import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReleaseTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SaerchForJsonAction extends ActionSupport implements Action,ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String saerchStr;
	
	HttpServletRequest request;
	
	public String getSaerchStr() {
		return saerchStr;
	}

	public void setSaerchStr(String saerchStr) {
		try {
			saerchStr = java.net.URLDecoder.decode(saerchStr, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}////解码关键！！！！ 
		this.saerchStr = saerchStr;
	}
	
	public String execute(){
		String path = request.getContextPath();
		ActionContext actionContext = ActionContext.getContext(); 
		HttpServletResponse response = (HttpServletResponse)actionContext.get(ServletActionContext.HTTP_RESPONSE); 
		
		ReleaseOS releaseOS = new ReleaseOS();
		
		List<ReleaseTB> saerchList = releaseOS.saerchForJSON(saerchStr, 0, 20);
		
		
		response.setContentType("text/xml; charset=gb2312");
		
		releaseOS.releaseConnection();
		response.setCharacterEncoding("gb2312");
		try {
			response.getWriter().write("<table>");
			for(ReleaseTB releaseTB:saerchList){
				String eachTr ="<tr><td>" +
					"<a href=\"view.action?id=" +releaseTB.getRelease_id() +"\">" +releaseTB.getRelease_subject()+
					"</a>"+ 
					"</td></tr>";
				
				response.getWriter().write(eachTr);
			}
			if(saerchList.size() == 0){
				response.getWriter().write("<tr><td><font color=\"red\">找不到相关数据</font></td></tr>");
			}
			response.getWriter().write("</table>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return NONE;
	}
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		
	}
}
